package com.game.controller;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.game.Urls;
import com.game.dto.PointDetailInfo;
import com.game.util.GsonUtils;
import com.game.util.MyConfig;
import com.game.util.ResultInfo;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

@RequestMapping("/user")
@Controller
public class UserController {

	private static final Logger log = LoggerFactory.getLogger(UserController.class);
	@Autowired
	private OkHttpClient okHttpClient;
	@Autowired
	private MyConfig config;

	@ResponseBody
	@RequestMapping("/createRoom")
	public ResultInfo createRoom(){
		Request request = new Request.Builder().url(config.getBizServiceIp()+Urls.CREATE_ROOM).build();
		try {
			Response response = okHttpClient.newCall(request).execute();
			String result = response.body().string();
			log.debug("调用创建房间接口返回：{}",result);
			return GsonUtils.gsonToBean(result, ResultInfo.class);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error("调用创建房间错误：{}",e);
		}
		return ResultInfo.ERROR;
	}
	@ResponseBody
	@RequestMapping("/detailMsg")
	public ResultInfo detailMsg(String pointId){
		Request request = new Request.Builder().url(config.getBizServiceIp()+Urls.DETAIL_POINT+"?pointId="+pointId).build();
		try {
			Response response = okHttpClient.newCall(request).execute();
			String result = response.body().string();
			log.debug("调用创建房间接口返回：{}",result);
			PointDetailInfo info =  GsonUtils.gsonToBean(result, PointDetailInfo.class);
			ResultInfo rs = ResultInfo.SUCCESS;
			rs.setResult(info);
			return rs;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error("调用查看红包详情错误：{}",e);
		}
		return ResultInfo.ERROR;
	}
	@RequestMapping("/chat")
	public String chat(){
		return "im";
	}
	
}
